package com.xzakota.minesweeper.widget;

import com.xzakota.minesweeper.model.entity.UserBean;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * 游戏排行榜视图
 *
 * @author Xzakota
 */
public class GameRankListPanel extends JPanel {
    private final JPanel scrollViewportPanel;

    public GameRankListPanel(List<UserBean> rankUsers) {
        setLayout(new BorderLayout());

        scrollViewportPanel = new JPanel();
        scrollViewportPanel.setLayout(new BoxLayout(scrollViewportPanel, BoxLayout.Y_AXIS));

        updateRankList(rankUsers);

        JScrollPane scrollPanel = new JScrollPane(scrollViewportPanel);
        add(scrollPanel);
    }

    public void updateRankList(List<UserBean> rankUsers) {
        if (rankUsers != null) {
            for (Component component : scrollViewportPanel.getComponents()) {
                scrollViewportPanel.remove(component);
            }

            for (int i = 0; i < rankUsers.size(); i++) {
                UserBean userBean = rankUsers.get(i);
                UserInfoPanel userItem1Panel = new UserInfoPanel(userBean);
                String info2Tag = "用时: ";
                userItem1Panel.setInfo2Tag(info2Tag);
                userItem1Panel.setInfo2Value(String.valueOf(userBean.time));
                if (i == 0) {
                    userItem1Panel.setAccentColor(true);
                }
                scrollViewportPanel.add(userItem1Panel);
                scrollViewportPanel.add(Box.createVerticalStrut(20));
            }

            revalidate();
            repaint();
        }
    }
}
